package com.sddz.pacs.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.code.kaptcha.Constants;
import com.sddz.pacs.annotation.SysLogger;
import com.sddz.pacs.dto.JsonResult;
import com.sddz.pacs.entity.HealthUser;
import com.sddz.pacs.entity.ResidentVo;
import com.sddz.pacs.entity.UserResult;
import com.sddz.pacs.service.HealthUserService;
import com.sddz.pacs.service.UserResultService;
import com.sddz.pacs.support.Constant;
import com.sddz.pacs.support.ResultConstant;
import com.sddz.pacs.util.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author SunLi
 * @Date 2018/9/10
 */
@RestController
@Api(description = "居民登陆相关接口")
public class ResidentController {

    @Autowired
    private HealthUserService healthUserService;

    @Autowired
    private UserResultService userResultService;

    @Autowired
    private  HttpServletRequest request;



    @PostMapping(value = "/resident/login")
    @SysLogger(value = "居民登录",type = "居民登录日志管理")
    @ApiOperation(value="居民登录", notes="居民登录系统")
    @ApiImplicitParam(name = "HealthUser",value = "居民用户json对象",required = true)
    public JsonResult residentLogin(@RequestBody ResidentVo residentVo) throws Exception {


        try{
            String cap = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
            if(!cap.equalsIgnoreCase(residentVo.getCaptcha())){
                return JsonResult.failure("验证码输入有误，请重新输入！");
            }
        }catch (UnknownAccountException e) {
            return JsonResult.failure(e.getMessage());
        }
        EntityWrapper<HealthUser> ew = new EntityWrapper<>();
        ew.eq("NAME",residentVo.getName());
        ew.eq("ID_CARD",residentVo.getIdCard());
        HealthUser healthUser =  healthUserService.selectOne(ew);
        //把登录居民登陆信息保存到session
        request.getSession().setAttribute("healthUser",healthUser);

        if(healthUser==null){
            return JsonResult.failure(-1,ResultConstant.LOGIN_FAILURE);
        }
        return JsonResult.success(healthUser,ResultConstant.LOGIN_SUCCESS);
    }
    @GetMapping(value = "/resident/getPersonalMess/{idCard}")
    @SysLogger(value = "居民个人信息",type = "居民个人信息日志")
    @ApiOperation(value="居民个人信息", notes="查询居民个人信息")
    @ApiImplicitParam(name = "idCard",value = "居民用户json对象",required = true)
    public JsonResult getPersonalMess(@PathVariable("idCard") String idCard ) throws Exception {

        EntityWrapper<HealthUser> ew = new EntityWrapper<>();
        ew.eq("ID_CARD",idCard);
        HealthUser healthUser =  healthUserService.selectOne(ew);
        return JsonResult.success(healthUser,ResultConstant.RESIDENT_MESS_SUCCESS);
    }
    @GetMapping(value = "/resident/getPersonalDateList/{id}")
    @SysLogger(value = "居民列表信息",type = "居民个人列表信息日志")
    @ApiOperation(value="居民所有检查时间列表信息", notes="查询居民所有检查的时间列表")
    @ApiImplicitParam(name = "id",value = "居民用户json对象",required = true)
    public JsonResult getPersonalDateMessList(@PathVariable("id") String id ) throws Exception {
        EntityWrapper<UserResult> ew = new EntityWrapper<>();
        ew.eq("USER_ID",id);
        List<UserResult> healthUser =  userResultService.selectList(ew);
        return JsonResult.success(healthUser, ResultConstant.LIST_SUCCESS);
    }
    @PostMapping(value = "/resident/printing")
    @SysLogger(value = "居民个人一条信息详情",type = "居民查看一条信息详情日志")
    @ApiOperation(value="居民个人一条信息详情", notes="查  询居民个人信息详情")
    @ApiImplicitParam(name = "UserResult",value = "居民用户json对象",required = true)
    public JsonResult getPersonalDate(@RequestBody UserResult user ) throws Exception {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> map = new HashMap<>();
        //通过个人信息获取所有的检查记录
        UserResult userResult = userResultService.selectPersonalOne(user.getId(), formatter.format(user.getCheckDate()));
        if (userResult != null) {
            map.put("bAddr1", Constant.IMAGE_PREFIX+userResult.getbAddr1());
            map.put("bAddr2", Constant.IMAGE_PREFIX+userResult.getbAddr2());
            map.put("provingTime", userResult.getCheckDate());
            map.put("remark", userResult.getRemark());
            map.put("userId", userResult.getUserId());
            map.put("fileNumber", userResult.getFileNumber());
            EntityWrapper<HealthUser> ew1 = new EntityWrapper<>();
            ew1.eq("ID", user.getId());
            //获取个人信息
            HealthUser healthUser = healthUserService.selectOne(ew1);
            map.put("age", healthUser.getAge());
            map.put("createDate", healthUser.getCreateDate());
            map.put("gender", healthUser.getGender());
            map.put("homeAddr", healthUser.getHomeAddr());
            map.put("name", healthUser.getName());
            map.put("idCard", healthUser.getIdCard());
            map.put("id", healthUser.getId());
            map.put("stationId", healthUser.getStationId());
            map.put("tel", healthUser.getTel());
            return JsonResult.success(map, ResultConstant.DETAILS_MESS_SUCCESS);
        } else {
            return JsonResult.failure(ResultConstant.DETAILS_MESS_FAILURE);
        }


    }
}
